#include "file_bin.h"

int file_bin_store( FILE *f, buf *b )
{
	int ret;

	if ( b->data != NULL )
	{
		ret = fwrite( b->data, 1, b->size, f );
		if (ret<0)
		{
			ERROR("Cannot write data\n");
			return ret;
		}
	}

	return ret;
}

int file_bin_load( FILE *f, buf *b )
{
	//PRINT("\n");
	if (feof(f))
		return -1;
	
	//PRINT("\n");

	int ret;
	char d[MAX_BUF_SIZE];
	ret = fread( d, 1, MAX_BUF_SIZE, f );

	b->size = ret;
	//PRINT("\n");

	
	if (b->data != NULL)
	{
		free( b->data);
		//PRINT("\n");

	} else
	{
		b->data = malloc( ret );
		memcpy( b->data, d, ret );
		//PRINT("\n");

	}
	

	return ret;
}


